<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">

    <xsl:template name="formal.object.heading">
        <xsl:param name="object" select="."/>
        <xsl:param name="placement" select="'before'"/>
        <xsl:param name="formal.object.with.label" select="self::figure|self::table|self::example|self::equation"/>
        <xsl:param name="formal.object.with.label.markup" select="self::figure|self::table|self::equation"/>
        <!-- divide formal object heading and make it support formal.title.label.properties -->
        <fo:block xsl:use-attribute-sets="formal.title.properties">
            <xsl:choose>
                <xsl:when test="$placement = 'before'">
                    <xsl:attribute name="keep-with-next.within-column">always</xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:attribute name="keep-with-previous.within-column">always</xsl:attribute>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:if test="$formal.object.with.label">
                <!-- have label -->
                <xsl:element name="fo:inline" use-attribute-sets="formal.title.label.properties">
                    <xsl:call-template name="gentext">
                        <xsl:with-param name="key" select="local-name()"/>
                    </xsl:call-template>
                    <xsl:if test="formal.object.autolabel != '0'">
                        <!-- test if formal objects must be numbered is enabled -->
                        <xsl:if test="$formal.object.with.label.markup">
                            <!-- have label.markup -->
                            <xsl:text disable-output-escaping="yes">&#xa0;</xsl:text>
                            <xsl:apply-templates select="$object" mode="label.markup">
                                <xsl:with-param name="allow-anchors" select="1"/>
                            </xsl:apply-templates>
                        </xsl:if>
                    </xsl:if>
                    <xsl:value-of select="$xref.label-title.separator"/>
                </xsl:element>
            </xsl:if>
            <xsl:apply-templates select="$object" mode="title.markup">
                <xsl:with-param name="allow-anchors" select="1"/>
            </xsl:apply-templates>
        </fo:block>
    </xsl:template>

    <xsl:template name="formal.object.heading.old">
        <!-- this old template is for reference only -->
        <xsl:param name="object" select="."/>
        <xsl:param name="placement" select="'before'"/>

        <fo:block xsl:use-attribute-sets="formal.title.properties">
            <xsl:choose>
                <xsl:when test="$placement = 'before'">
                    <xsl:attribute name="keep-with-next.within-column">always</xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:attribute name="keep-with-previous.within-column">always</xsl:attribute>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:apply-templates select="$object" mode="object.title.markup">
                <xsl:with-param name="allow-anchors" select="1"/>
            </xsl:apply-templates>
        </fo:block>
    </xsl:template>

    <xsl:template name="formal.object">
        <!-- FBA : added support for formal.object.body.properties -->
        <xsl:param name="placement" select="'before'"/>

        <xsl:variable name="id">
            <xsl:call-template name="object.id"/>
        </xsl:variable>

        <xsl:variable name="content">
            <xsl:if test="$placement = 'before'">
                <xsl:call-template name="formal.object.heading">
                    <xsl:with-param name="placement" select="$placement"/>
                </xsl:call-template>
            </xsl:if>

            <xsl:element name="fo:block" use-attribute-sets="formal.object.body.properties">
                <xsl:apply-templates/>
            </xsl:element>

            <xsl:if test="$placement != 'before'">
                <xsl:call-template name="formal.object.heading">
                    <xsl:with-param name="placement" select="$placement"/>
                </xsl:call-template>
            </xsl:if>
        </xsl:variable>

        <xsl:variable name="keep.together">
            <xsl:call-template name="dbfo-attribute">
                <xsl:with-param name="pis" select="processing-instruction('dbfo')"/>
                <xsl:with-param name="attribute" select="'keep-together'"/>
            </xsl:call-template>
        </xsl:variable>

        <xsl:choose>
            <!-- tables have their own templates and 
         are not handled by formal.object -->
            <xsl:when test="self::figure">
                <fo:block id="{$id}" xsl:use-attribute-sets="figure.properties">
                    <xsl:if test="$keep.together != ''">
                        <xsl:attribute name="keep-together.within-column">
                            <xsl:value-of select="$keep.together"/>
                        </xsl:attribute>
                    </xsl:if>
                    <xsl:copy-of select="$content"/>
                </fo:block>
            </xsl:when>
            <xsl:when test="self::example">
                <fo:block id="{$id}" xsl:use-attribute-sets="example.properties">
                    <xsl:if test="$keep.together != ''">
                        <xsl:attribute name="keep-together.within-column">
                            <xsl:value-of select="$keep.together"/>
                        </xsl:attribute>
                    </xsl:if>
                    <xsl:copy-of select="$content"/>
                </fo:block>
            </xsl:when>
            <xsl:when test="self::equation">
                <fo:block id="{$id}" xsl:use-attribute-sets="equation.properties">
                    <xsl:if test="$keep.together != ''">
                        <xsl:attribute name="keep-together.within-column">
                            <xsl:value-of select="$keep.together"/>
                        </xsl:attribute>
                    </xsl:if>
                    <xsl:copy-of select="$content"/>
                </fo:block>
            </xsl:when>
            <xsl:when test="self::procedure">
                <fo:block id="{$id}" xsl:use-attribute-sets="procedure.properties">
                    <xsl:if test="$keep.together != ''">
                        <xsl:attribute name="keep-together.within-column">
                            <xsl:value-of select="$keep.together"/>
                        </xsl:attribute>
                    </xsl:if>
                    <xsl:copy-of select="$content"/>
                </fo:block>
            </xsl:when>
            <xsl:otherwise>
                <fo:block id="{$id}" xsl:use-attribute-sets="formal.object.properties">
                    <xsl:if test="$keep.together != ''">
                        <xsl:attribute name="keep-together.within-column">
                            <xsl:value-of select="$keep.together"/>
                        </xsl:attribute>
                    </xsl:if>
                    <xsl:copy-of select="$content"/>
                </fo:block>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="informal.object">
        <!-- FBA : added support for informal.object.body.properties -->
        <xsl:variable name="id">
            <xsl:call-template name="object.id"/>
        </xsl:variable>

        <xsl:variable name="keep.together">
            <xsl:call-template name="dbfo-attribute">
                <xsl:with-param name="pis" select="processing-instruction('dbfo')"/>
                <xsl:with-param name="attribute" select="'keep-together'"/>
            </xsl:call-template>
        </xsl:variable>

        <xsl:variable name="content">
            <xsl:element name="fo:block" use-attribute-sets="informal.object.body.properties">
                <xsl:apply-templates/>
            </xsl:element>
        </xsl:variable>

        <xsl:choose>
            <!-- informaltables have their own templates and 
         are not handled by formal.object -->
            <xsl:when test="local-name(.) = 'equation'">
                <fo:block id="{$id}" xsl:use-attribute-sets="equation.properties">
                    <xsl:if test="$keep.together != ''">
                        <xsl:attribute name="keep-together.within-column">
                            <xsl:value-of select="$keep.together"/>
                        </xsl:attribute>
                    </xsl:if>

                    <xsl:copy-of select="$content"/>

                </fo:block>
            </xsl:when>
            <xsl:when test="local-name(.) = 'procedure'">
                <fo:block id="{$id}" xsl:use-attribute-sets="procedure.properties">
                    <xsl:if test="$keep.together != ''">
                        <xsl:attribute name="keep-together.within-column">
                            <xsl:value-of select="$keep.together"/>
                        </xsl:attribute>
                    </xsl:if>

                    <xsl:copy-of select="$content"/>

                </fo:block>
            </xsl:when>
            <xsl:when test="local-name(.) = 'informalfigure'">
                <fo:block id="{$id}" xsl:use-attribute-sets="informalfigure.properties">
                    <xsl:if test="$keep.together != ''">
                        <xsl:attribute name="keep-together.within-column">
                            <xsl:value-of select="$keep.together"/>
                        </xsl:attribute>
                    </xsl:if>

                    <xsl:copy-of select="$content"/>

                </fo:block>
            </xsl:when>
            <xsl:when test="local-name(.) = 'informalexample'">
                <fo:block id="{$id}" xsl:use-attribute-sets="informalexample.properties">
                    <xsl:if test="$keep.together != ''">
                        <xsl:attribute name="keep-together.within-column">
                            <xsl:value-of select="$keep.together"/>
                        </xsl:attribute>
                    </xsl:if>

                    <xsl:copy-of select="$content"/>

                </fo:block>
            </xsl:when>
            <xsl:when test="local-name(.) = 'informalequation'">
                <fo:block id="{$id}" xsl:use-attribute-sets="informalequation.properties">
                    <xsl:apply-templates/>
                </fo:block>
            </xsl:when>
            <xsl:otherwise>
                <fo:block id="{$id}" xsl:use-attribute-sets="informal.object.properties">
                    <xsl:if test="$keep.together != ''">
                        <xsl:attribute name="keep-together.within-column">
                            <xsl:value-of select="$keep.together"/>
                        </xsl:attribute>
                    </xsl:if>

                    <xsl:copy-of select="$content"/>

                </fo:block>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
